package studentPackage;

import java.util.*;
import java.io.*;

public class StudentSystem {
	
	public boolean search(HashSet<Student> studentSet, Student student) {
		return studentSet.contains(student);
	}
	
	public void addStudent(HashSet<Student> studentSet, Student student) {
		if (studentSet.contains(student)) {
			System.out.println("Studentul exista deja in set!");
		} else  {
		studentSet.add(student);
		}
	}
	
	public void removeStudent(HashSet<Student> studentSet, Student student) {
		studentSet.remove(student);
	}

	public static void main(String[] args) {
		
		StudentSystem studentSystem = new StudentSystem();
		ConcreteStudentCreator concreteStudentCreator = new ConcreteStudentCreator();
		HashSet<Student> studentSet = concreteStudentCreator.createStudents();
		
		UndergraduateStudent undergraduate = new UndergraduateStudent("Mihai", "Ivan", 
				false, false, false, false, 3, 0, 2);
		GraduateStudent graduate = new GraduateStudent("Stefan", "Mihailescu", 
				true, true, 15, 1);
		PartTimeStudent partTime = new PartTimeStudent("Lucian", "Nita", true, 
				false, 4, 3);
		PartTimeStudent partTimeGraduate = new PartTimeStudent("Andrei", "Lungu", 
				true, true, 6, 2);
		
		UndergraduateStudent test = new UndergraduateStudent("ss", "dd", false, false, true, true, 4, 0, 3);
		
		/*Verificare daca un obiect e continut sau nu in set*/
		
		System.out.println(studentSystem.search(studentSet, undergraduate)); /*true*/
		System.out.println(studentSystem.search(studentSet, graduate)); /*true*/
		System.out.println(studentSystem.search(studentSet, partTime)); /*true*/
		System.out.println(studentSystem.search(studentSet, partTimeGraduate)); /*true*/
		System.out.println(studentSystem.search(studentSet, test)); /*false*/
		System.out.println();
		
		/*Stergere student si verificare a stergerii corecte*/
		
		studentSystem.removeStudent(studentSet, undergraduate);;
		System.out.println("undergraduate after remove: " + studentSet.contains(undergraduate)); /*false*/
		
		System.out.println();
		
		/*Creare si adaugare student*/
		
		GraduateStudent newGraduate = new GraduateStudent("Alexandru", "Luca", false, true, 14, 1);
		studentSystem.addStudent(studentSet, newGraduate);
		System.out.println("newGraduate: " + studentSet.contains(newGraduate));
		
		System.out.println();
		
		/*Modificare student*/
		
		studentSet.remove(graduate);
		System.out.println("graduate after remove: " + studentSet.contains(graduate));
		graduate.setYearOfStudy(2);
		studentSet.add(graduate);
		System.out.println("graduate after insert: " + studentSet.contains(graduate));
		
		System.out.println();
		
		/*Afisare ore de lucru pentru diferite tipuri de studenti*/
		
		System.out.println("Undergraduate: " + undergraduate.getHoursPerWeek() + " hours");
		System.out.println("Graduate: " + graduate.getHoursPerWeek() + " hours");
		System.out.println("PartTime: " + partTime.getHoursPerWeek() + " hours");
		System.out.println("PartTimeGraduate: " + partTimeGraduate.getHoursPerWeek() + " hours");
		
		System.out.println();
		
		/*Scrierea unui obiect de tip student in fisier*/
		
		FileOutputStream fos = null;
		ObjectOutputStream oos = null;
		
		/*Exception handling pe doua niveluri*/
		
		try {
			try {
				fos = new FileOutputStream("studentOutput.txt");
				oos = new ObjectOutputStream(fos);
				oos.writeObject(undergraduate);
			}
			catch(IOException e) {
				throw new Exception("Nu s-a putut executa operatiunea");
			}
			
			finally {
				if(oos != null) {
					oos.close();
				}
				
				if(fos != null) {
					fos.close();
				}
			}
		}
		catch(Exception e) {
			System.err.println(e.getCause());
		}
		
		/*Citire din fisierul creat anterior*/
		
		FileInputStream fis = null;
		ObjectInputStream ois = null;
		
		try {
			fis = new FileInputStream("studentOutput.txt");
			ois = new ObjectInputStream(fis);
			UndergraduateStudent gs = (UndergraduateStudent)ois.readObject();
			System.out.println(gs.getClass());
			System.out.println("Succes!");
		}
		
		catch(IOException e) {
			System.out.println(e.getMessage());
		}
		
		catch(ClassNotFoundException e) {
			System.out.println(e.getMessage());
		}
		
		finally {
			try {
				ois.close();
				fis.close();
			}
			catch(IOException e){
				System.out.println(e.getMessage());
			}
			
		}
	}

}
